package com.example.location;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.example.baidumapsum.R;
import com.example.baidumapsum.R.drawable;
import com.example.baidumapsum.R.id;
import com.example.baidumapsum.R.layout;

/**
 * @ClassName:
 * @Description: 显示我的位置
 * @author gongxibo
 * @date 创建时间：2015年12月18日 下午5:22:25
 */
public class ShowMylocationActivity extends FragmentActivity {
	protected GeoCoder mSearch = null; // 搜索模块，也可去掉地图模块独立使用
	protected BaiduLocation baiduLoc;
	/**
	 * MapView 是地图主控件
	 */
	MapView mMapView = null;
	/**
	 * 对地图view进行操作，
	 */
	BaiduMap mBaiduMap = null;
	protected Context context;
	boolean isFirstLoc = true;// 是否首次定位
	/**
	 * 我的位置
	 */
	private View myLocation;


	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(getLayoutId());
		context = this;
		initView();
		initListener();
	}

	protected void initListener() {
		myLocation.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(context, "正在定位....", Toast.LENGTH_SHORT).show();
				baiduLoc.requestLocation();
				
			}
		});

	}

	/**
	 * 初始化view
	 */
	protected void initView() {
		setTitle("我的位置");
		myLocation=findViewById(R.id.my_location);
		initMapView();
		startLocation();

	}

	/**
	 * 初始化地图
	 */
	protected void initMapView() {
		// 地图初始化
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		// 定位到上海
		// mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(GEO_SHANGHAI));
		// 设置地图比例尺，18级。比例尺为200m
		mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(16));
		// 开启定位图层,显示我的位置图标
		mBaiduMap.setMyLocationEnabled(true);

	}

	/**
	 * 开始定位
	 */
	protected void startLocation() {
		// 百度获取经纬度
		baiduLoc = new BaiduLocation();
		baiduLoc.getmLocationClient(context).registerLocationListener(
				new BDLocationListener() {

					@Override
					public void onReceiveLocation(BDLocation location) {
						// map view 销毁后不在处理新接收的位置
						if (location == null || mMapView == null) {
							return;
						}
						MyLocationData locData = new MyLocationData.Builder()
								.accuracy(location.getRadius())
								// 此处设置开发者获取到的方向信息，顺时针0-360
								.direction(100)
								.latitude(location.getLatitude())
								.longitude(location.getLongitude()).build();
						mBaiduMap.setMyLocationData(locData);
						System.out.println("纬度：" + String.valueOf(location.getLatitude())
								+ "\n 经度：" + String.valueOf(location.getLongitude())
								+ "\n地址 = " + location.getAddrStr());
//						if (isFirstLoc) {
//							isFirstLoc = false;
							LatLng ll = new LatLng(location.getLatitude(),
									location.getLongitude());
							MapStatusUpdate u = MapStatusUpdateFactory
									.newLatLng(ll);
							mBaiduMap.animateMapStatus(u);

//						}
					}
				});
	}

	protected int getLayoutId() {

		return R.layout.activity_map;
	}

	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (null != baiduLoc) {
			// 退出时销毁定位
			baiduLoc.stopLocation();
		}
		// 关闭定位图层
		mBaiduMap.setMyLocationEnabled(false);
		mMapView.onDestroy();
		mMapView = null;
		super.onDestroy();
	}

	/**
	 * 经纬度转地址
	 * 
	 * @param arg0
	 */
	protected void transLatLngToAddress(LatLng latlong) {
		// 初始化搜索模块，注册事件监听
		mSearch = GeoCoder.newInstance();
		mSearch.setOnGetGeoCodeResultListener(onResultListener);
		// 反Geo搜索
		mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(latlong));

	}

	private OnGetGeoCoderResultListener onResultListener = new OnGetGeoCoderResultListener() {

		@Override
		public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
			if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
				Toast.makeText(context, "抱歉，未能找到结果", Toast.LENGTH_LONG).show();
				return;
			}
			mBaiduMap.clear();
			mBaiduMap.addOverlay(new MarkerOptions().position(
					result.getLocation()).icon(
					BitmapDescriptorFactory
							.fromResource(R.drawable.icon_gcoding)));
			// 地图移到中间
			// mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
			// .getLocation()));
			// AddressComponent comp = result.getAddressDetail();
			// 区。比如：闵行区
			// String sss = comp.district;

			Toast.makeText(context, result.getAddress(), Toast.LENGTH_LONG)
					.show();

		}

		@Override
		public void onGetGeoCodeResult(GeoCodeResult arg0) {
			// TODO Auto-generated method stub

		}
	};
}
